//
//  ChannelDataHelper.swift
//  DoctorVisits
//
//  Created by lanou3g on 16/3/1.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

typealias BlockChannel = (_ :Array<ChannelFirstViewModel>) ->Void
typealias BlockChannel2 = (_ :Array<ChannelSecondViewModel>) -> Void

class ChannelDataHelper: NSObject {

    var dataArray = Array<ChannelFirstViewModel>()
    var dataArray2 = Array<ChannelSecondViewModel>()
    
    private override init() {}
    static let shareInstance = ChannelDataHelper()
    
    func requestAllModelWithUrlString(urlString:String,success:BlockChannel) ->Void{
    
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            do{
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let dict:NSArray = dic["data"] as! NSArray
                for allDict in dict{
                 let firstV = ChannelFirstViewModel()
                    firstV.setValuesForKeysWithDictionary(allDict as! [String : AnyObject])
                    print(firstV.vt1)
                    self.dataArray.append(firstV)
                }
            }catch {
            
            }
//            print("幼稚吗\(self.dataArray.count)")
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.dataArray)
                
            })
        
        })
        task.resume()
    }
    
    func requestSecondModelWithUrlString(urlString:String,success:BlockChannel2) ->Void{
        
        self.dataArray2.removeAll()
        
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            do{
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let dict:NSArray = dic["data"] as! NSArray
                for allDict in dict{
                    let secondV = ChannelSecondViewModel()
                    secondV.setValuesForKeysWithDictionary(allDict as! [String : AnyObject])
                    //                    print(firstV.subname)
                    self.dataArray2.append(secondV)
                }
            }catch {
                
            }
            //            print("幼稚吗\(self.dataArray.count)")
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.dataArray2)
                
            })
            
        })
        task.resume()
    }

    
    func returnModel(index:Int)->ChannelFirstViewModel {
//        print(self.dataArray[index].subname)
        return self.dataArray[index]
    }
    
}
